package com.jianying.day04;

import org.apache.flink.api.common.functions.Partitioner;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * ClassName: Flink02_Part
 * Package: com.atguigu.day04
 * Description:
 *
 * @Author: tubage
 * @Create: 2024/4/2 09:33
 * @Version: 1.0
 */
public class Flink02_Part {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(new Configuration());
        env.setParallelism(4);
        env.disableOperatorChaining();

        DataStreamSource<String> socketDS = env.socketTextStream("localhost", 8888);
//        socketDS.shuffle().print();

//        socketDS.rebalance().print();
//        socketDS.rescale().print();
//        socketDS.broadcast().print();
//        socketDS.map(value -> value).global().print();
//        socketDS.map(value -> value).keyBy(value -> value).print();
//        socketDS.map(value -> value).map(value -> value).print();

        socketDS.partitionCustom(new MyPartitioner(), a -> a).print();
        env.execute();
    }
}

class MyPartitioner implements Partitioner<String> {
    @Override
    public int partition(String s, int i) {
        return s.hashCode() % i;
    }
}
